import 'package:address_picker/address_manager.dart';
import 'package:address_picker/address_model.dart';
import 'package:address_picker/address_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_stort/config/routes.dart';
import 'package:flutter_stort/utils/jump_util.dart';

class DialogUtils {
  ///显示公共对话框
  ///context    上下文对象
  ///_title     标题
  ///_content   内容
  ///_onPressed 确定监听
  static void showCommonDialog(BuildContext context, String _title,
      String _content, VoidCallback _onPressed) {
    showDialog(
      barrierDismissible: false,
      context: context,
      builder: (context) => CustomDialog(
        contextView: CommonDialog(
          title: _title,
          content: _content,
          onPressed: _onPressed,
        ),
      ),
    );
  }

  ///显示软件评分对话框
  static void showSoftwareScore(BuildContext context) {
    showDialog(
      context: context,
      barrierDismissible: true,
      builder: (context) => CustomDialog(
        contextView: Container(
          height: 260,
          width: 300,
          padding: EdgeInsets.only(top: 20),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Icon(
                CupertinoIcons.hand_thumbsup,
                color: Colors.red,
                size: 50,
              ),
              SizedBox(height: 10),
              Expanded(
                child: Text(
                  "批评鼓励一下",
                  style: TextStyle(
                    color: Colors.red,
                    fontSize: 18,
                  ),
                ),
              ),
              Divider(color: Colors.grey, height: 0.5),
              FlatButton(
                minWidth: 300,
                height: 50,
                onPressed: () {
                  JumpPageUtils.closeDialog(context);
                },
                child: Text(
                  "喜欢？马上去评价",
                  style: TextStyle(
                    color: Colors.red,
                    fontSize: 16,
                  ),
                ),
              ),
              Divider(color: Colors.grey, height: 0.5),
              FlatButton(
                height: 50,
                minWidth: 300,
                onPressed: () {
                  JumpPageUtils.closeDialog(context);
                  JumpPageUtils.startNext(context, RoutesUtils.FEEDBACK_ROOT);
                },
                child: Text(
                  "体验差，我要吐槽",
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 16,
                  ),
                ),
              ),
            ],
          ),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(10),
            color: Colors.white,
          ),
        ),
      ),
    );
  }

  ///显示地址选择
  static void showSelectAddress(
      BuildContext context, OnSelectCallback onSelectCallback) {
    Address address;
    AddressManager.loadAddressData(context).then((value) {
      AddressProvince _selectedProvince = value.first;
      AddressCity _selectedCity = _selectedProvince.cities.first;
      AddressDistrict _selectedDistrict = _selectedCity.district.first;
      address = Address(
        currentProvince: _selectedProvince,
        currentCity: _selectedCity,
        currentDistrict: _selectedDistrict,
      );
    });
    showDialog(
      context: context,
      builder: (context) => CustomBottomDialog(
        contextView: Container(
          height: 300,
          width: double.infinity,
          color: Colors.white,
          child: Column(
            children: [
              Container(
                height: 50,
                child: Row(
                  children: [
                    FlatButton(
                      onPressed: () => JumpPageUtils.closeDialog(context),
                      child: Text(
                        "取消",
                        style: TextStyle(
                          color: Colors.grey,
                          fontSize: 18,
                        ),
                      ),
                    ),
                    Expanded(flex: 1, child: Container()),
                    FlatButton(
                      onPressed: () {
                        JumpPageUtils.closeDialog(context);
                        onSelectCallback(address);
                      },
                      child: Text(
                        "确定",
                        style: TextStyle(
                          color: Colors.greenAccent,
                          fontSize: 18,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              BottomSheet(
                onClosing: () {},
                builder: (context) => Container(
                  height: 250.0,
                  child: AddressPicker(
                    style: TextStyle(color: Colors.black, fontSize: 17),
                    mode: AddressPickerMode.provinceCityAndDistrict,
                    onSelectedAddressChanged: (value) {
                      address = value;
                    },
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

typedef OnSelectCallback = void Function(Address);

///公共对话框
class CommonDialog extends StatelessWidget {
  String title;
  String content;
  VoidCallback onPressed;

  CommonDialog({this.title, this.content, @required this.onPressed});

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 300,
      height: 230,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            height: 50,
            alignment: Alignment.center,
            child: Text(
              title,
              style: TextStyle(
                color: Colors.black,
                fontSize: 18,
              ),
            ),
          ),
          Divider(
            color: Colors.grey,
            height: 0.5,
          ),
          Expanded(
            child: Container(
              alignment: Alignment.topLeft,
              padding: EdgeInsets.all(10),
              child: Text(
                content,
                maxLines: 5,
                overflow: TextOverflow.ellipsis,
                textAlign: TextAlign.left,
                style: TextStyle(
                  color: Colors.grey,
                  fontSize: 14,
                ),
              ),
            ),
          ),
          Divider(
            color: Colors.grey,
            height: 0.5,
          ),
          Container(
            height: 50,
            child: Row(
              children: [
                Expanded(
                  child: FlatButton(
                    height: 50,
                    onPressed: () {
                      JumpPageUtils.closeDialog(context);
                    },
                    child: Text(
                      "取消",
                      style: TextStyle(
                        color: Colors.black,
                        fontSize: 16,
                      ),
                    ),
                  ),
                ),
                Container(
                  color: Colors.grey,
                  width: 0.5,
                ),
                Expanded(
                  child: FlatButton(
                    height: 50,
                    onPressed: onPressed,
                    child: Text(
                      "确定",
                      style: TextStyle(
                        color: Colors.black,
                        fontSize: 16,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
      ),
    );
  }
}

///底部按钮对话框
class CustomBottomDialog extends Dialog {
  Widget contextView;

  CustomBottomDialog({this.contextView});

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.black.withOpacity(0.5),
      type: MaterialType.transparency,
      child: Container(
        alignment: Alignment.bottomCenter,
        child: contextView,
      ),
      elevation: 1.0,
    );
  }
}

///自定义对话框
class CustomDialog extends Dialog {
  Widget contextView;

  CustomDialog({this.contextView});

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.black.withOpacity(0.5),
      type: MaterialType.transparency,
      child: Center(
        child: contextView,
      ),
      elevation: 1.0,
    );
  }
}
